package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.dao.EwsRoleDao;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.WorkOrderLogDao;
import com.egoo.ticket.server.dao.WorkOrderTypeNodeDao;
import com.egoo.ticket.server.pojo.dto.WorkOrderLogDto;
import com.egoo.ticket.server.pojo.entity.EwsUser;
import com.egoo.ticket.server.pojo.entity.WorkOrderLog;
import com.egoo.ticket.server.pojo.vo.WorkOrderLogVo;
import com.egoo.ticket.server.pojo.vo.WorkOrderTypeNodeVo;
import com.egoo.ticket.server.service.EripDataSynService;
import com.egoo.ticket.server.service.WorkOrderLogService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.CreateCodeUtil;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderLogServiceImpl implements WorkOrderLogService {
	@Autowired
	WorkOrderLogDao workOrderLogDao;
	@Autowired
	WorkOrderTypeNodeDao workOrderTypeNodeDao;
	@Autowired
	EwsRoleDao ewsRoleDao;
	@Autowired
	EripDataSynService eripDataSynService;
	@Autowired
	private CreateCodeUtil createCodeUtil;
	@Autowired
	private EwsUserDao ewsUserDao;

	/**
	 * TODO 获取工单日志列表
	 * 
	 * @author: victor_tang
	 * @createtime: 2019/10/5 21:22
	 * @param workOrderLogDto 1
	 * @return: java.utils.List<com.egoo.ews.service.vo.WorkOrderLogVo>
	 */
	@Override
	public DbResponse getWorkOrderList(WorkOrderLogDto workOrderLogDto) {
		// 设置分页参数
		PageHelper.startPage(workOrderLogDto.getCurrentPage(), workOrderLogDto.getPageSize());
		// 获取日志列表
		List<WorkOrderLogVo> dataList =
				workOrderLogDao.selectWorkOrderLogList(workOrderLogDto);
		// 获取日志总数
		PageInfo<WorkOrderLogVo> pageInfo = new PageInfo<>(dataList);

		// 组装用户角色名称
		for (WorkOrderLogVo vo : dataList) {
			// 组装用户角色，创建人
			if (ConstantCode.SYSTEM_OPERATION_LOGO.equals(vo.getLogCreaterId())) {
				vo.setLogCreaterName(ConstantCode.SYSTEM_OPERATION_LOGO_NAME);
			}
			vo.setCreateUserRoleNameStr("操作人");

			// 翻译字段
			String logTypeView = translate(vo.getLogType());
			if (logTypeView != null) {
				vo.setLogType(logTypeView);
			} else {
				vo.setLogType(vo.getLogType());
			}
			if (ConstantCode.WorkOrderLogType.REASSIGN_NAME.equals(vo.getLogType())&&
					StringUtils.isNotEmpty(vo.getContent())) {
				int i = vo.getContent().indexOf(ConstantCode.REASSIGN_CONTENT_TAG);
				if (i!=-1) {
					vo.setContent(vo.getContent().substring(0,i));
				}
			}
		}
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, ResponseConstant.SELECT_SUCCESS_MSG, dataList,
				pageInfo.getTotal());
	}

	/**
	 * TODO 翻译日志类型
	 * @author: victor_tang
	 * @createtime: 2020/5/20 13:31
	 * @param logtype 1
	 * @return: java.lang.String
	 */
	private String translate(String logtype) {

		if (ConstantCode.WorkOrderLogType.LOG_TYPE_ADDITIONAL_COMMENTS.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_ADDITIONAL_COMMENTS_ONE;
		}
		else if (ConstantCode.WorkOrderLogType.LOG_TYPE_REMINDER.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_REMINDER_TWO;
		}
		else if (ConstantCode.WorkOrderLogType.LOG_TYPE_TIMEOUT.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_TIMEOUT_THREE;
		}
		else if (ConstantCode.WorkOrderLogType.LOG_TYPE_RETURN_VISIT.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_RETURN_VISIT_FOUR;
		}
		else if (ConstantCode.WorkOrderLogType.LOG_TYPE_ERROR_MARKER.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_ERROR_MARKER_FIVE;

		}
		else if (ConstantCode.WorkOrderLogType.LOG_TYPE_IS_RESPONSIBILITI.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.LOG_TYPE_IS_RESPONSIBILITI_SIX;
		}
		else if (ConstantCode.WorkOrderLogType.MONITOR_WORK_ORDER_CODE.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.MONITOR_WORK_ORDER_NAME;
		}
		else if (ConstantCode.WorkOrderLogType.CANCEL_WORK_ORDER_CODE.equals(logtype)) {
			return ConstantCode.WorkOrderLogType.CANCEL_WORK_ORDER_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.OVERRULED_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.OVERRULED_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.MSG_SEND_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.MSG_SEND_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.REASSIGN_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.REASSIGN_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.WITHDRAW_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.WITHDRAW_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.CLOSE_ORDER_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.CLOSE_ORDER_NAME;
		}
		else if(ConstantCode.WorkOrderLogType.DELETE_ORDER_CODE.equals(logtype)){
			return ConstantCode.WorkOrderLogType.DELETE_ORDER_NAME;
		}
		else {
			return null;
		}
	}

	/**
	 * TODO 工单日志新增
	 */
	@Override
	public DbResponse workOrderLogAdd(WorkOrderLog workOrderLog) {
		EwsUser ewsUser = ewsUserDao.selectById(workOrderLog.getLogCreaterId());
		workOrderLog.setLogCreaterName(ewsUser!=null?ewsUser.getName():"");
		switch (workOrderLog.getLogType()) {
		case ConstantCode.WorkOrderLogType.LOG_TYPE_ADDITIONAL_COMMENTS:
			// 追加日志
			return additionalComments(workOrderLog);

		case ConstantCode.WorkOrderLogType.LOG_TYPE_ERROR_MARKER:
			// 差错标记
			return errorMarker(workOrderLog);

		case ConstantCode.WorkOrderLogType.LOG_TYPE_IS_RESPONSIBILITI:
			// 是否有责
			return isResponsibility(workOrderLog);

		default:
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, ResponseConstant.ADD_FAIL_MSG);
		}
	}

	/**
	 * TODO 新增工单日志
	 * 
	 * @author: victor_tang
	 * @createtime: 2019/10/5 21:23
	 * @param workOrderLog 1
	 * @return: java.lang.Integer
	 */
	@Override
	public Integer addSave(WorkOrderLog workOrderLog) {
		workOrderLog.setId(createCodeUtil.getWorkOrderLogId());
		workOrderLog.setLogCreateTime(new Date());
		if (workOrderLog.getWorkOrderNodeId()!=null) {
			WorkOrderTypeNodeVo workOrderTypeNodeVo = workOrderTypeNodeDao
					.getNodeByWorkOrderNodeId(workOrderLog.getWorkOrderNodeId());
			workOrderLog.setWorkOrderNodeName(workOrderTypeNodeVo!=null?
					workOrderTypeNodeVo.getName():null);
		}
		return workOrderLogDao.insert(workOrderLog);
	}

	/**
	 * TODO 差错标记
	 */
	private DbResponse errorMarker(WorkOrderLog workOrderLog) {

		int result = addSave(workOrderLog);

		if (result > 0) {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "差错标记成功！");
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE, "差错标记失败！");
	}

	/**
	 * TODO 追加日志
	 */
	private DbResponse additionalComments(WorkOrderLog workOrderLog) {
		int result = addSave(workOrderLog);

		if (result > 0) {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "追加日志成功！");
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE, "追加日志失败！");
	}

	/**
	 * TODO 是否有责
	 */
	private DbResponse isResponsibility(WorkOrderLog workOrderLog) {
		int result = addSave(workOrderLog);

		if (result > 0) {
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE, "是否有责成功！");
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE, "是否有责失败！");
	}

}
